package taskRunner

import "time"

type Worker struct {
	ticker *time.Ticker //接收从系统发送过来的时间
	runner *Runner
}

func NewWorker(interval time.Duration, r *Runner) *Worker {
	return &Worker{
		ticker: time.NewTicker(interval * time.Second),
		runner: r,
	}
}

func (w *Worker) startWorker() {
	//TODO 理解如何使用ticker实现，range取ticker为什么会有很大的误差？
	for {
		select {
		case <-w.ticker.C:
			go w.runner.StartAll()
		}
	}
}

func Start() {
	//start video file cleaning
	r := NewRunner(3, true, VideoClearDispatcher, VideoClearExecutor)
	//注意这里传入的是函数句柄而不是函数执行之后的结果
	w := NewWorker(3, r) //这里为什么可以实现设置为3s
	go w.startWorker()
}
